package com.mutouren.modules.datapush.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mutouren.common.log.LogAlias;
import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.modules.datapush.dao.ConcurrentStrategyDao;
import com.mutouren.modules.datapush.dao.impl.DaoFactoryImpl;
import com.mutouren.modules.datapush.strategy.ConcurrentStrategy;

public class ConcurrentStrategyConfig {
	
	private static ConcurrentStrategyDao concurrentStrategyDao = DaoFactoryImpl.getInstance().getConcurrentStrategyDao();

	private volatile static Map<String, ConcurrentStrategy> mapConcurrentStrategy = new HashMap<String, ConcurrentStrategy>();
	
	private final static Logger errorLogger = LogManager.getLogger(LogAlias.SystemError.name());
	private final static Logger runLogger = LogManager.getLogger(LogAlias.SystemRun.name());		
	
	static {
		load();
	}
	
	private ConcurrentStrategyConfig(){};
	
	private synchronized static void setConfig(Map<String, ConcurrentStrategy> map) {
		mapConcurrentStrategy = map;
	}
	
	public static void load() {
		try {
			runLogger.info("ConcurrentStrategyConfig.load begin");
			Map<String, ConcurrentStrategy> map = new HashMap<String, ConcurrentStrategy>(); 
			
			List<ConcurrentStrategy> listStrategy = concurrentStrategyDao.select(null);
			for(ConcurrentStrategy strategy : listStrategy) {
				map.put(strategy.getMethodAlias().trim(), strategy);
			}
			
			setConfig(map);
			runLogger.info("ConcurrentStrategyConfig.load end");
		} catch(Throwable t) {
			errorLogger.error("ConcurrentStrategyConfig.load error", t);
			throw t;
		}
	}
	
	public static ConcurrentStrategy getConcurrentStrategy(String methodAlias) {
		return mapConcurrentStrategy.get(methodAlias);
	}
}


